faApp.controller('LoginCtrl',['$scope','loginService','$location','$controller','ajaxService','sessionService',function($scope,loginService,$location,$controller,ajaxService,sessionService) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.checkBrowserEnv();
   
	$scope.login = function(){

		$scope.form.loginName.$dirty = true;
		$scope.form.password.$dirty = true;

		if ($scope.form.$valid) {

			var promise = loginService.login($scope.user);

			promise.then(function(data) {
				if (data.statusCode) {
					if ("0001" == data.statusCode) {
						$scope.markFieldError($scope.form.password,"passerror");
            return;
					}
				} else {
					$scope.setLogined(data); 
          sessionService.setItem("user",angular.toJson($scope.loginStatus) || "");
          sessionService.setItem("user.access",data.access || "");
          if (data.access == "all"){
            sessionService.setItem("user.groups",data.access || "");
          }else{
            var groups = [];
            angular.forEach(data.groups || [],function(o){
              groups.push(o.groupId);
            });
            sessionService.setItem("user.groups",groups || "");
          }

          $location.path("/index");
				}
			}, function(data) {
				
			});
		}
	};
	$scope.upUserInfo = function(){
		$scope.form.oldPassword.$dirty = true;
		$scope.form.newPassword.$dirty = true;
		$scope.form.telePhone.$dirty = true;
		$scope.form.email.$dirty = true;

		if($scope.user.newPassword != $scope.user.oldPassword){
			alert('两次密码不一致');
			return
		}
		if ($scope.form.$valid) {
			$scope.user.password = $scope.user.newPassword;
			var promise = loginService.updateLoginInfo($scope.user);

			promise.then(function(data) {
				//从登陆看，走到这一步就算登陆成功了
				alert('修改密码成功');
				$location.path("/index");

			}, function(data) {

			});
		}
	}

}]);

faApp.controller('LogoutCtrl',['$scope','loginService','$location','$controller','ajaxService',function($scope,loginService,$location,$controller,ajaxService) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  loginService.logout().then(function(){
    $scope.logout();
    $location.path("/login");
  });
}]);
